<?php
namespace app\common\model;
use x\Xbase;
class User extends Xbase
{

    /**
     * 保存用户信息
     * [saveUser description]
     * @param  [type] $d [description]
     * @return [type]    [description]
     */
    public function saveUser($d){
        $data = [
            "username" => $d['username'],
            "name" => $d['name'],
            "sex" => $d['sex'],
            "update_time" => time()
        ];
        $userid = $d["user_id"];
        $result = $this->db->update("user",$data,["id=?",[$userid]]);
        //保存银行
        $userbank = $this->db->query("select * from user_bank where user_id=?",[$userid],1);
        $bankdata = [
            "user_id" => $userid,
            "user_name" => $d['name'],
            "bank_name" => $d['bankname'],
            "bank_wangdian" => $d['bankuser'],
            "bank_no" => $d['banknumber'],
            "update_time" => time()
        ];
        if(count($userbank) > 0){//已存在
            return $this->db->update("user_bank",$bankdata,["id=?",[$userbank['id']]]);
        }else{
            $bankdata["create_time"] = time();
            return $this->db->insert('user_bank',$bankdata);
        }
    }


    //是否登录
    public function isLogin(){
        $token = cookie('token');
        if($token){
            if($this->m=='www'){
                $sql = "select * from user where token=?";
            }else{
                $sql = "select * from user where wap_token=?";
            }
            $rs = $this->db->query($sql,[$token],1);
            return $rs ? $rs : [];
        }
        return [];
    }

    /**
     * 登录
     * @param  [type] $username [description]
     * @param  [type] $password [description]
     */
  	public function login($username, $password){
        /* 获取用户数据 */
        $user = $this->db->query('select * from user where username=? or phone=?',[$username,$username],1);
        if (empty($user) || (int)$user['status'] !== 0) {
            $this->setError('用户不存在或被禁用','username');
            return false;
        }
        if($user['password'] != $this->com_password($password,$user['salt'])){
            $this->setError('密码错误','password');
            return false;
        }
        $token = token($user['id']);
        $ip = ip();
        $data = [
            'id' => $user['id'],
            'last_login_ip' => $ip,
            'last_login_time' => time(),
            'login_times' => $user['login_times']+1,
            'update_time' => time(),
        ];
        if($this->m=='www'){
            $type = 0;
            $data['token'] = $token;
        }else{
            $type = 1;
            $data['wap_token'] = $token;
        }
        $this->db->insert('user_login',['user_id'=>$user['id'],'ip'=>$ip,'type'=>$type]);
        $this->db->update('user',$data);
        cookie('token',$token);
        return $token;
  	}

    /**
     * 注册
     * [register description]
     * @param  [type] $username  [description]
     * @param  [type] $userphone [description]
     * @param  [type] $password  [description]
     * @return [type]            [description]
     */
    public function register($username, $userphone,$password){
        /* 获取用户数据 */
        $user = $this->db->query('select * from user where username=?',[$username],1);
        if ($user) {
            $this->setError('用户已存在','username');
            return false;
        }
        /* 获取用户数据 */
        $user = $this->db->query('select * from user where phone=?',[$userphone],1);
        if ($user) {
            $this->setError('该手机号已注册','username');
            return false;
        }
        $salt = rand(1000,9999);
        $data = [
            'username' => $username,
            'password' => $this->com_password($password,$salt),
            'phone' => $userphone,
            'reg_ip' => ip(),
            'salt' => $salt,
            'create_time' => time(),
            'sex' => 1
        ];
        $this->db->insert('user',$data);
        return true;
    }

    /**
     * 重置密码
     * [forget description]
     * @param  [type] $userphone [description]
     * @param  [type] $password  [description]
     * @return [type]            [description]
     */
    public function forget($userphone,$password){
        /* 获取用户数据 */
        $user = $this->db->query('select * from user where phone=?',[$userphone],1);
        if (!$user) {
            $this->setError('账号不存在','username');
            return false;
        }
        $data = [
            'password' => $this->com_password($password,$user['salt']),
            'update_time' => time()
        ];
        $this->db->update('user',$data,["phone=?",[$userphone]]);
        return true;
    }

    //加密方式
    public function com_password($password,$salt){
        return md5(md5($password).$salt);
    }
}
